package Command;

import java.util.Vector;

import DTO.Row;
import XML.XMLHelper;

public class Delete extends Command {
	
	public Delete(String str)
	{
		super(str);
	}
	
	public Object executeQuery() 
	{		
		Object ret = null;
		try
		{
			String sql = getSql();
			
			// Check delete keyword				
			String str1 = sql.trim();
			if(str1.startsWith("delete"))
			{
				// Check from command
				int intoIndex = sql.indexOf("delete") + "delete".length();
				String str2 = sql.substring(intoIndex).trim();			
				if(str2.startsWith("from"))
				{
					int tableIndex = sql.indexOf("from") + "from".length();
					String str3 = sql.substring(tableIndex).trim();
					String[] strs = str3.split(" ", 2);				
					String tableName = strs[0];
					
					if(strs.length > 1)
					{
						String str4 = strs[1].trim();
						if(str4.startsWith("where"))
						{
							int conditionIndex = str4.indexOf("where") + "where".length();
							String condition = str4.substring(conditionIndex);
							if(Condition.checkParentheses(condition) &&
									Condition.mayBeCondition(condition))
							{
								Vector<Row> rows = Condition.ReversePoland(condition, getDbName(), tableName);
														
								int rowDelete = 0;
								for(int i = 0 ; i < rows.size(); i++)
								{					
																	
									if(XMLHelper.DeleteRow(getDbName(), tableName, rows.get(i)))
										rowDelete++;
								}
								
								if(rowDelete > 0)
								{
									ret = "Delete " + rowDelete + " row(s) !";
									System.out.println(ret);
								}
							}
							
							/*
							String[] opers = Condition.getOperandsAndOperator(condition);
							String leftOperand = opers[0];
							String rightOperand = opers[2];
							String operator = opers[1];
							
							ret = deleteOneCondition(ret, tableName, leftOperand,
									rightOperand, operator);
							*/
						}
					}
					else
					{
						// Delete all from table "tableName" ( use XMLHelper
						Vector<Row> deleteRows = XMLHelper.GetListOfRow(getDbName(), tableName);
						int rowDelete = 0;
						
						for(int i = 0 ; i < deleteRows.size(); i++)
						{
							if(XMLHelper.DeleteRow(getDbName(), tableName, deleteRows.get(i)))
								rowDelete++;
						}
						
						if(deleteRows != null && deleteRows.size() > 0)
							ret = "Delete " + rowDelete + " row(s) !";
						
					}
				}
			}
				
			if(ret == null)
				ret = "Delete failed!";
			return ret;
		}
		catch (Exception e) {
			// TODO: handle exception
			ret = "Delete failed!";
			return ret;
		}
		
	}

/*
	private Object deleteOneCondition(Object ret, String tableName,
			String leftOperand, String rightOperand, String operator) {
		// Get list of fields name
		Vector<Field> fields = XMLHelper.GetListOfField(getDbName(), tableName);
		for(int i = 0 ; i < fields.size(); i++)
			if(leftOperand == fields.get(i).get_Name())
			{
				int dataType = fields.get(i).get_Type();
				Vector<Row> deleteRow = new Vector<Row>();
				Vector<Row> rows = XMLHelper.GetRows(getDbName(), tableName, leftOperand);
				for(int j = 0 ; j < rows.size(); j++)
				{
					switch(operator)
					{
					case ">":
						if(dataType == 0)
						{
							int rowValue = Integer.parseInt((String) rows.get(j).get_Fileds().get(fields.get(i)));
							int right = Integer.parseInt(rightOperand);
							if(rowValue > right)
							{
								deleteRow.add(rows.get(j));
							}
						}
						else
						{
							String rowValue = (String) rows.get(j).get_Fileds().get(fields.get(i));
							
							if(rowValue.compareTo(rightOperand) > 0)
							{
								deleteRow.add(rows.get(j));
							}
						}
						break;
					case "<":
						if(dataType == 0)
						{
							int rowValue = Integer.parseInt((String) rows.get(j).get_Fileds().get(fields.get(i)));
							int right = Integer.parseInt(rightOperand);
							if(rowValue < right)
							{
								deleteRow.add(rows.get(j));
							}
						}
						else
						{
							String rowValue = (String) rows.get(j).get_Fileds().get(fields.get(i));
							
							if(rowValue.compareTo(rightOperand) < 0)
							{
								deleteRow.add(rows.get(j));
							}
						}
						break;
					case ">=":
						if(dataType == 0)
						{
							int rowValue = Integer.parseInt((String) rows.get(j).get_Fileds().get(fields.get(i)));
							int right = Integer.parseInt(rightOperand);
							if(rowValue >= right)
							{
								deleteRow.add(rows.get(j));
							}
						}
						else
						{
							String rowValue = (String) rows.get(j).get_Fileds().get(fields.get(i));
							
							if(rowValue.compareTo(rightOperand) >= 0)
							{
								deleteRow.add(rows.get(j));
							}
						}
						break;
					case "<=":
						if(dataType == 0)
						{
							int rowValue = Integer.parseInt((String) rows.get(j).get_Fileds().get(fields.get(i)));
							int right = Integer.parseInt(rightOperand);
							if(rowValue <= right)
							{
								deleteRow.add(rows.get(j));
							}
						}
						else
						{
							String rowValue = (String) rows.get(j).get_Fileds().get(fields.get(i));
							
							if(rowValue.compareTo(rightOperand) <= 0)
							{
								deleteRow.add(rows.get(j));
							}
						}
						break;
					case "=":
						if(dataType == 0)
						{
							int rowValue = Integer.parseInt((String) rows.get(j).get_Fileds().get(fields.get(i)));
							int right = Integer.parseInt(rightOperand);
							if(rowValue == right)
							{
								deleteRow.add(rows.get(j));
							}
						}
						else
						{
							String rowValue = (String) rows.get(j).get_Fileds().get(fields.get(i));
							
							if(rowValue.compareTo(rightOperand) == 0)
							{
								deleteRow.add(rows.get(j));
							}
						}
						break;
					}
				}
				
				int rowDelete = 0;
				for(int j = 0; j < deleteRow.size(); j++)
				{
					if (XMLHelper.DeleteRow(getDbName(), tableName, deleteRow.get(j)))
						rowDelete++;
				}
				
				if(rowDelete > 0)
				{
					ret = "Delete " + rowDelete + " row ";
				}								
			}
		return ret;
	}
	*/
}
